package com.mkl.system.controller;

import com.mkl.system.common.Constant;
import com.mkl.system.common.DataGridView;
import com.mkl.system.common.ResultObj;
import com.mkl.system.domain.Role;
import com.mkl.system.service.RoleService;
import com.mkl.system.vo.RoleVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;

/**
 * @author king
 * @version 1.0
 * @date 2020/3/24 17:00
 */
@RestController
@RequestMapping("api/role")
public class RoleController {

    @Autowired
    private RoleService roleService;

    /**
     * 查询所有权限
     *
     * @param roleVo vo
     * @return 返回结果
     */
    @RequestMapping("loadAllRole")
    public Object loadAllRole(RoleVo roleVo) {
        return roleService.queryAllRole(roleVo);
    }

    /**
     * 新增角色权限
     *
     * @param role role
     * @return 返回结果
     */
    @RequestMapping("addRole")
    public ResultObj addRole(Role role) {
        try {
            role.setCreatetime(new Date());
            role.setAvailable(Constant.AVAILABLE_TRUE);
            roleService.save(role);
            return ResultObj.ADD_SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            return ResultObj.ADD_ERROR;
        }
    }

    /**
     * 删除
     *
     * @param id 删除id
     * @return 返回删除结果 200
     */
    @RequestMapping("deleteRole")
    public ResultObj deleteRole(Integer id) {
        try {
            roleService.removeById(id);
            return ResultObj.DELETE_SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            return ResultObj.DELETE_ERROR;
        }
    }

    /**
     * 修改角色
     *
     * @param role 角色
     * @return 返回结果
     */
    @RequestMapping("updateRole")
    public ResultObj updateRole(Role role) {
        try {
            roleService.updateById(role);
            return ResultObj.UPDATE_SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            return ResultObj.UPDATE_ERROR;
        }
    }

    /**
     * 批量删除角色
     *
     * @param ids 角色ids
     * @return 返回结果
     */
    @RequestMapping("batchDeleteRole")
    public ResultObj batchDeleteRole(Integer[] ids) {
        try {
            for (Integer id : ids) {
                roleService.removeById(id);
            }
            return ResultObj.DELETE_SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            return ResultObj.DELETE_ERROR;
        }
    }

    /**
     * 根据角色id查询角色拥有的菜单和权限id
     *
     * @param id 角色id
     * @return 返回菜单id
     */
    @RequestMapping("queryMenuIdsByRid")
    public Object queryMenuIdsByRid(Integer id) {
        List<Integer> mids = roleService.queryMenuIdsByRid(id);
        return new DataGridView((long) mids.size(), mids);
    }

    /**
     * 保存菜单与角色之间的关系
     *
     * @param rid 角色id
     * @param mids 菜单ids
     * @return 结果
     */
    @RequestMapping("saveRoleMenu")
    public ResultObj saveRoleMenu(Integer rid, Integer[] mids) {
        try {
            roleService.saveRoleMenu(rid, mids);
            return ResultObj.DISPATCH_SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            return ResultObj.DISPATCH_ERROR;
        }
    }

    @RequestMapping("loadAllAvailableRoleNoPage")
    public Object loadAllAvailableRoleNoPage(RoleVo roleVo){
        roleVo.setAvailable(Constant.AVAILABLE_TRUE);
        return roleService.queryAllAvailableRoleNoPage(roleVo);
    }

}
